package cn.com.egova.demoapplication.base;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import cn.com.egova.demoapplication.R;
import cn.com.egova.demoapplication.inter.ISelectListener;

public class FuncListActivity extends Activity implements AdapterView.OnItemClickListener {
    public ListView lv;
    /**功能列表数据*/
    public String[] data;
    public ISelectListener listener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentViewID());

        setBackIconVisible(isBackIconVisible());
        initData();
        initView();
    }

    public int getContentViewID(){
        return R.layout.activity_funclist;
    }

    /**
     * 可初始化一些变量,如列表数据,监听事件等.
     */
    public void initData(){

    }

    public void initView() {
        lv = (ListView) findViewById(R.id.lv);
        if(data!=null && data.length!=0){
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,android.R.id.text1,data);
            lv.setAdapter(adapter);
        }
        lv.setOnItemClickListener(this);
    }


    /**
     * 设置是否在菜单栏显示返回按钮图标.
     * @param isVisible
     */
    public void setBackIconVisible(boolean isVisible){
        ActionBar actionBar = getActionBar();
        if(null != actionBar) {
            actionBar.setDisplayHomeAsUpEnabled(isVisible);
            actionBar.setDisplayShowHomeEnabled(false); //默认不显示应用程序图标.
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(listener!=null){
            listener.onSelect(view, position);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public String[] getData() {
        return data;
    }

    public void setData(String[] data) {
        this.data = data;
    }

    public ISelectListener getListener() {
        return listener;
    }

    public void setListener(ISelectListener listener) {
        this.listener = listener;
    }

    public boolean isBackIconVisible() {
        return false;
    }
}

